package com.fuqi.android.zhongcai.business.shuig.view.activity;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.view.activity.BaseActivity;
import com.fuqi.android.zhongcai.base.view.adapter.BaseRecyclerViewAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.MonthOrderAdapter;
import com.fuqi.android.zhongcai.business.shuig.request.api.QueryOrderListByDateApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.QueryOrderListByDateResult;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.TimeUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class MonthPerOrderListActivity extends BaseActivity {

    @Bind(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private MonthOrderAdapter orderCustomerAdapter;
    private List<QueryOrderListByDateResult> orderList;

    @Override
    public void initView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(MonthPerOrderListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        orderList = new ArrayList<>();
        orderCustomerAdapter = new MonthOrderAdapter(this,orderList);
        mRecyclerView.setAdapter(orderCustomerAdapter);

    }

    @Override
    public void initListener() {

        orderCustomerAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<QueryOrderListByDateResult>() {
            @Override
            public void onItemClick(View view, int position, QueryOrderListByDateResult data) {

                Bundle bundle = new Bundle();
                bundle.putString("order_id",data.getORDER_ID());
                bundle.putString("order_type", Constants.TYPE_SHUI_GUAN);
                ActivityUtils.startActivity(MonthPerOrderListActivity.this,ShuiGActivity.class,bundle);
//                ActivityUtils.startActivity(MonthPerOrderListActivity.this,OrderDetailActivity.class);
            }
        });

        mRecyclerView.setLoadingMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(false);

    }

    @Override
    public void initData() {

        if (getIntent().getStringExtra("title") != null) {
            titleBar.setTitle(getIntent().getStringExtra("title"));
        }

        RequestManager.getInstance().call(new QueryOrderListByDateApi(new QueryOrderListByDateApi.QueryOrderListByDateParams(
                getIntent().getStringExtra("order_type"),
                UniApplication.getInstance().getUserInfo().getUserid(),
                TimeUtils.getMonthFirstDayDate(),
                TimeUtils.getTommorwDate()), respOrderListener, errorListener));

    }

    Response.Listener<QueryOrderListByDateResult[]> respOrderListener = new Response.Listener<QueryOrderListByDateResult[]>() {

        @Override
        public void onResponse(QueryOrderListByDateResult[] response) {

            if (HandleCode.requestSuccess()) {
                orderList.clear();
                for(int i = 0; i < response.length ;i++) {
                    response[i].setORDER_TYPE(getIntent().getStringExtra("order_type"));
                    orderList.add(response[i]);
                }
                orderCustomerAdapter.notifyDataSetChanged();
                Log.i("juner", "juner response respJCYListener: " + GsonHelper.writeValue(response));
            } else {


            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };



    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_month_per_order_list;
    }
}
